import * as Blockly from 'blockly/core'

// 1.-------------------------获取路由参数----------------------------------------

const getCurrentUserJson = {
  type: 'current_user_info',
  message0: '获取当前登录用户信息',
  output: null,
  colour: 160,
  tooltip: '获取当前登录用户信息',
  helpUrl: ''
}
// 创建积木
Blockly.Blocks['current_user_info'] = {
  init: function () {
    this.jsonInit(getCurrentUserJson)
  }
}

Blockly.JavaScript['current_user_info'] = function (block) {
  const code = `context.getCurrentUser()`
  return [code, Blockly.JavaScript.ORDER_COMMA]
}

// 2. -----------消息 Message --------------------
Blockly.Extensions.register('dynamic_msg_extension',
  function () {
    const dropdown = new Blockly.FieldDropdown(
      function () {
        var options = []
        options = [
          ['成功', 'success'],
          ['错误', 'error'],
          ['警告', 'warning'],
          ['消息', 'info']
        ]
        return options
      })
    // 生成extension的block
    this.getInput('MESSAGE_TYPE')
      .appendField(dropdown, 'Type')
  })

const messageJson = {
  type: 'message_text',
  message0: '提示 %1 消息 %2',
  args0: [
    // 动态生成选项
    {
      'type': 'input_dummy',
      'name': 'MESSAGE_TYPE'
    },
    {
      'type': 'field_input',
      'name': 'MESSAGE',
      'text': '消息内容',
      'check': 'String'
    }
  ],
  extensions: ['dynamic_msg_extension'],
  // output: '',
  previousStatement: null,
  nextStatement: null,
  inputsInline: true,
  style: 'tool_blocks',
  tooltip: '',
  helpUrl: ''
}

// 创建积木
Blockly.Blocks['message_text'] = {
  init: function () {
    this.jsonInit(messageJson)
  }
}
Blockly.JavaScript['message_text'] = function (block) {
  let code = `context.Message(`
  const type = block.getFieldValue('Type')
  const text = Blockly.JavaScript.quote_(block.getFieldValue('MESSAGE' ))|| '\'\''
  code = code + `{\n type: '${type}',\n message: ${text}\n}`
  code = code + ')\n'
  return code
}


